
layui.use(['laytpl','element', 'form', 'carousel', 'layer', 'table','laydate'], function() {
	var element = layui.element;
	var carousel = layui.carousel;
	var form = layui.form;
	var laytpl = layui.laytpl;
	var layer = layui.Layer;
	var $ = layui.$
	var table = layui.table;
	var laydate = layui.laydate;
	var studentNo;
	var paperId;

	//取sessionStorage里面的userKey

	var studentInfo = document.getElementById("studentInfo").innerHTML;
	var teacherList = document.getElementById("teacherList").innerHTML;
	var responseList = document.getElementById("responseList").innerHTML;
	var userKey = JSON.parse(sessionStorage.getItem("userKey"));
	console.log(userKey)
	if(userKey) {
		//如果存在,则说明登录过,先比对过期时间
		var delayTime = userKey.delayTime
		var nowTime = new Date().getTime()
		if(nowTime < delayTime) {
			//登录未失效
			laytpl(studentInfo).render(userKey, function(html) {
				$("#studentInfoView").html(html)
				element.render()
				$.ajax({
					type: "get",
					url: "http://localhost:8888/teacher/getTeacherList",
					success: function(data) {
						console.log(data)					
						laytpl(teacherList).render(data, function(html) {
							$("#teacherListView").html(html)
							element.render()
						})	
					}
				});
				
				$.ajax({
					type: "get",
					url: "http://localhost:8888/msg/getApplicationResByStudentNo",
					data:{"studentNo":userKey.studentNo},
					success: function(data) {
						console.log(data)
						laytpl(responseList).render(data, function(html) {
							$("#responseListView").html(html)
							element.render()
						})
					}
				});	
			})
		}
	}
	function formatDate(date) {
		dates = date.split("/");
		if(dates.length == 3) {
			if(dates[1].length == 1) {
				dates[1] = "0" + dates[1];
			}
			if(dates[2].length == 1) {
				dates[2] = "0" + dates[2];
			}
			date = dates.join("-");
			return date;
		} else {
			return null;
		}
	}
	
	function parseTime(timestamp) {
		var date = new Date(parseInt(timestamp)).toLocaleDateString();　　 //输出结果为2016/8/9
		date = formatDate(date);　　 //输出结果为2016-08-09，满足YYYY-MM-DD格式要求
		return date;
	}

	//渲染表格
	var table = layui.table;
	var userKey = JSON.parse(sessionStorage.getItem("userKey"));
	table.render({
		elem: '#score',
		url: 'http://localhost:8888/studentPaper/paperOfStudentDetail/' + userKey.studentNo //数据接口
			,
		page: false //开启分页
			,
		cols: [
			[ //表头
				{
					field: 'id',
					title: 'ID',
					width: 80,
					sort: true,
					fixed: 'left'
				}, {
					field: 'paperId',
					title: '试卷id',
					width: 80
				}, {
					field: 'title',
					title: '试卷名称',
					width: 200
				}, {
					field: 'isDone',
					title: '完成情况',
					templet: function(d) {
						return d.isDone == 0 ? '<a href="exam.html"><span style="color:red">未完成</sapn></a>' : '<span style="color:green">完成</sapn>'
					},
					width: 120,
					sort: true
				}, {
					field: 'score',
					title: '分数',
					width: 80
				}, {
					field: 'comment',
					title: '教师评语'
				}
			]
		]
	});
	
	//提交申请
	layui.define(function(exports) {
	//do something
	
exports('application', function(id) {
	
		var date = document.getElementById("date").innerHTML;
		var freeDate = '';
		form.on('submit(freeTimeSubmit)', function(data) {
			var freeTimeData = data
			var freeTime = $("input[name='freeTime']:checked").val()
			var teacherId = $("input[name='teacherId']").val()
			console.log(freeTime)
			console.log(teacherId)
			console.log(freeDate)
			$.ajax({
				type: "post",
				url: "http://localhost:8888/teacherFreeTime/setFreeTime",
				data: {"teacherId":teacherId,"freeDate":freeDate,"freeTime":freeTime},
				success: function(data) {
					
					$.ajax({
						type:"post",
						url:"http://localhost:8888/application/apply",
						data:{"teacherId":teacherId,"studentNo":userKey.studentNo,"time":freeDate+" "+freeTime},
						success:function(data){
							layui.layer.msg("预约成功,请在预约信息里查看回复")
							location.reload();
						},
						error:function(){
							layui.layer.msg("未知错误")
						}
					});
					//layui.layer.msg("预约成功,请在预约信息里查看回复")
					
				},
				error: function() {
					layui.layer.msg('预约失败', {
						anim: 6
					});
				}
			});
		
			return false;
		});
		layui.layer.open({
			type: 1,
			skin: 'layui-layer-rim', //加上边框
			//宽高
			title: "请确认预约日期",
			content: date
		});
		

		layui.use(['laydate','form'], function() {
			var form = layui.form
			
			
			
			
		layui.laydate.render({
				elem: '#dateOption',				
				max:7,
				min:0,
				change: function(value, date) { //监听日期被切换
	
				},
				done: function(value, date, endDate) {
					console.log(value)
					var userKey = JSON.parse(sessionStorage.getItem("userKey"));
					var dateSelect = document.getElementById("dateSelect").innerHTML;
					freeDate = value
					$.ajax({
						type:"post",
						url:"http://localhost:8888/teacherFreeTime/selectFreeTimeOnDate",
						data:{"teacherId":id,"freeDate":value},
						success:function(data){
							laytpl(dateSelect).render(data, function(html) {
								$("#dateSelectView").html(html)
								element.render()
							})
						}
					});
							
				}
			});
		})
	
	});
	});
	
	
	
	
	

	//确认回复 删除 回复
	layui.define(function(exports) {
		//do something
	
		exports('delete', function(id) {
			layui.$.ajax({
				type: "post",
				url: "http://localhost:8888/msg/deleteMsgById",
				data: {
					"id": id
				},
				success: function(data) {
					layui.layer.msg("删除成功")
					location.reload()
				}
			});
		})
	});
});

	




//退出方法
function logout() {
	sessionStorage.clear();
	window.history.href = '../login.html'
}

	